<template>
  <van-action-sheet
    v-model="visible"
    :close-on-click-overlay="false"
    close-on-popstate
    :overlay="false"
    get-container=".addCar"
  >
    <div class="vnp-header">
      <button type="button" class="vnp-btn-finish" @click="finish">完成</button>
    </div>

    <!-- <div class="vnp-input-box-outer">
      <vnp-input-box
        ref="inputBox"
        v-model="val"
        editable
        @activeChange="handleActiveChange"
      ></vnp-input-box>
    </div> -->

    <div class="vnp-keys">
      <div class="vnp-keys-row" v-for="(item, index) in list" :key="index">
        <div
          class="vnp-btn-key-wrapper"
          v-for="(val, index) in item"
          :key="index"
          :class="{
            'vnp-del-wrapper': val === 'del',
            'vnp-type-wrapper': val === 'type',
          }"
        >
          <van-button
            v-if="val === 'type'"
            class="vnp-btn-key"
            @click="handleChangeType"
          >
            <span v-if="type === 'cn'"
              >中/<span class="vnp-smaller">英</span></span
            >
            <span v-else><span class="vnp-smaller">中</span>/英</span>
          </van-button>

          <van-button
            v-else-if="val === 'del'"
            class="vnp-btn-key"
            @click="handleDel"
          >
            <van-icon name="close" class="btn_icon" />
          </van-button>

          <van-button
            v-else
            class="vnp-btn-key"
            :class="{ 'vnp-btn-empty': !val }"
            @click="handleClickKey(val)"
          >
            {{ val }}
          </van-button>
        </div>
      </div>
    </div>
  </van-action-sheet>
</template>
  
  <script>
import Box from "./input.vue";

export default {
  components: {
    "vnp-input-box": Box,
  },
  name: "vnp-keyboard",
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    value: {
      type: String,
      default: "",
    },
    keybordType: {
      type: String,
      default: "cn",
    },
  },
  data() {
    return {
      val: this.value,
      type: "cn",
      cn: [
        ["京", "津", "沪", "渝", "冀", "豫", "云", "辽", "黑", "湘"],
        ["皖", "鲁", "新", "苏", "浙", "赣", "鄂", "桂", "甘", "晋"],
        ["蒙", "陕", "吉", "闽", "贵", "粤", "青", "藏", "川", "宁"],
        ["type", "琼", "使", "领", "学", "", "", "", "del"],
      ],
      en: [
        ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"],
        ["Q", "W", "E", "R", "T", "Y", "U", "O", "P"],
        ["A", "S", "D", "F", "G", "H", "J", "K", "L"],
        ["type", "Z", "X", "C", "V", "B", "N", "M", "del"],
      ],
    };
  },
  computed: {
    visible: {
      set(val) {
        this.$emit("update:show", val);
      },
      get() {
        return this.show;
      },
    },
    list() {
      return this.type === "en" ? this.en : this.cn;
    },
  },
  watch: {
    show() {
      if (this.show) {
        this.val = this.value;
      }
    },
    keybordType(newVal) {
      this.type = newVal;
    },
  },
  methods: {
    handleChangeType() {
      this.type = this.type === "en" ? "cn" : "en";
    },
    handleClickKey(val) {
      if (val) {
        // this.$refs.inputBox.setValue(val);
        this.$emit("input", val);
      }
    },
    handleActiveChange(activeIndex) {
      if (activeIndex === 0) {
        this.type = "cn";
      } else {
        this.type = "en";
      }
    },
    handleDel() {
      //   this.$refs.inputBox.del();
      this.$emit("del");
    },
    finish() {
      this.visible = false;
    },
  },
};
</script>
  
  <style lang="less" scoped>
.vnp-header {
  min-height: 80px;
  padding: 10px 0;
  position: relative;

  .vnp-btn-finish {
    position: absolute;
    right: 0;
    height: 100%;
    padding: 0 16px;
    color: red;
    font-size: 44px;
    background-color: transparent;
    border: none;
    cursor: pointer;
  }
}

.vnp-input-box-outer {
  width: 82%;
  max-width: 600px;
  margin: 0 auto;
  padding: 10px;
}

.vnp-keys {
  padding: 3px;
  background: #f2f3f5;
  padding-bottom: 22px;

  .vnp-keys-row {
    display: flex;
    justify-content: center;
  }
  .vnp-btn-key-wrapper {
    flex: 0 1 calc((100% - 6px * 10) / 10);
    padding: 3px;
    box-sizing: content-box;

    &.vnp-del-wrapper,
    &.vnp-type-wrapper {
      flex: 1;
    }
    &.vnp-type-wrapper {
      .vnp-smaller {
        color: #999;
        font-size: 12px;
      }
    }

    .vnp-btn-key {
      padding: 0;
      width: 100%;
      border-radius: 4px;
    }
    .vnp-btn-empty {
      background: transparent;
      border: none;
    }
    .vnp-delete-icon {
      width: 18px;
      vertical-align: middle;
    }
  }
}

.btn_icon {
  font-size: 44px;
}
</style>